import { toFixed } from '@/utils'
import { locSelCard } from '@/utils/locStore/data'
import mixinIndex from '../mixin/index'
export default {
  methods: {
    listData() {
      const list = []
      this.checkPackageItem &&
        this.checkPackageItem.chooseNum &&
        list.push({
          name: this.packageInfo(),
          value: '￥' + this.packageMoney(),
          type: '0'
        })
      if (this.orderInfo.hasCommercePrice) {
        if (this.differenceTotal()) {
          list.push({
            name: '电影票',
            value: this.filmPrice,
            more: this.differenceText(),
            type: '1'
          })
        } else {
          list.push({ name: '电影票', value: this.filmPrice, type: '0' })
        }
      }

      this.needPayMoney() &&
        list.push({
          name: '影票补差',
          value: '￥' + this.needPayMoney(),
          type: '0'
        })
      this.servicePrice() &&
        list.push({
          name: '技术服务费',
          value: '￥' + this.servicePrice(),
          type: '0'
        })
      this.cardMoney() &&
        list.push({
          name: '会员卡支付',
          value: '-￥' + this.cardMoney(),
          type: '0'
        })
      console.log('listData', list)
      return list
    },

    packageMoney() {
      return toFixed(
        this.checkPackageItem.saleTotalPrice * this.checkPackageItem.chooseNum
      )
    },
    cardMoney() {
      const card = locSelCard.get() || null
      let money = 0
      if (this.checkActive === 'discountRule' && card) {
        money =
          Number(this.discountRule.totalPrice) +
          this.discountRule.technicalServiceFee * this.ticketNum
      }
      return money
    },
    differenceText() {
      if (this.checkActive === 'discountRule') {
        return `特惠价   省${this.differenceTotal()}元`
      } else {
        return `券抵扣   -${this.differenceTotal()}, 省${this.couponMoney()}元`
      }
    },

    differenceTotal() {
      const coupons = this.coupons || []
      const len = this.coupons.length
      //特惠价
      if (this.checkActive === 'discountRule') {
        return this.discountRule.benefitPrice
      }

      if (!len && !(this.checkPackageItem && this.checkPackageItem.chooseNum)) {
        return ''
      }

      let differenceTotal = 0
      if (this.coupons.length > 0) {
        //有用券
        const isType1 = coupons.find((i) => i.couponType === '1')
        if (isType1) {
          //兑换券
          // differenceTotal =
          //   differenceTotal - this.$utils.arrayCalc(coupons, 'difference')
          differenceTotal = this.orderInfo.singleSalePrice * len
        } else {
          //优惠券
          // const reduceTotal = this.$utils.arrayCalc(coupons, 'cutReduce')
          const reduceTotal = mixinIndex.methods.getAfterUserCouponPrice()
          differenceTotal = reduceTotal
        }
      }
      // const differenceTotal = this.getAfterUserCouponPrice()
      if (len === Number(this.ticketNum)) {
        //用券完全支付
        return toFixed(differenceTotal)
      } else {
        //用券不足支付再用套票
        if (this.checkPackageItem && this.checkPackageItem.chooseNum) {
          differenceTotal +=
            this.orderInfo.singleSalePrice * (this.ticketNum - len)
        }
        return toFixed(differenceTotal)
      }
    },
    couponMoney() {
      let couponMoney = this.filmPrice
      const coupons = this.coupons || []
      if (this.coupons.length > 0) {
        const differenceTotal = this.$utils.arrayCalc(coupons, 'difference')
        const unitSaleMoney = this.$utils.arrayCalc(coupons, 'unitSaleMoney')
        couponMoney = couponMoney - unitSaleMoney - differenceTotal
      }
      if (coupons.length === Number(this.ticketNum)) {
        return toFixed(couponMoney)
      } else {
        if (this.checkPackageItem && this.checkPackageItem.chooseNum) {
          couponMoney =
            couponMoney -
            (+this.checkPackageItem.saleTotalPrice +
              +this.checkPackageItem.diffPrice) *
              (this.ticketNum - coupons.length)
        }
        return toFixed(couponMoney)
      }
    },
    //影票用券需要补差的金额
    needPayMoney() {
      if (this.checkActive === 'discountRule') {
        return 0
      }
      let needPayMoney = 0
      const coupons = this.coupons || []
      if (this.coupons.length > 0) {
        needPayMoney = this.$utils.arrayCalc(coupons, 'difference')
      }
      if (coupons.length === Number(this.ticketNum)) {
        return needPayMoney - this.servicePrice()
      } else {
        if (this.checkPackageItem && this.checkPackageItem.chooseNum) {
          this.checkPackageItem.saleTotalPrice
          needPayMoney =
            needPayMoney +
            this.checkPackageItem.diffPrice * (this.ticketNum - coupons.length)
        }
        return toFixed(needPayMoney - this.servicePrice())
      }
    },
    packageInfo() {
      return `${this.checkPackageItem.name}  (${this.checkPackageItem.couponNum}张) X ${this.checkPackageItem.chooseNum}`
    },
    servicePrice() {
      const len = this.coupons.length
      //用特惠价
      if (this.checkActive === 'discountRule') {
        return this.discountRule.technicalServiceFee * this.ticketNum
      }

      if (!len && !(this.checkPackageItem && this.checkPackageItem.chooseNum)) {
        //没用券并且没买套票
        return 0
      }

      let technicalServiceFee = 0
      if (this.coupons.length > 0) {
        this.coupons.forEach((item) => {
          technicalServiceFee += +item.technicalServiceFee
        })
      }
      // const differenceTotal = this.getAfterUserCouponPrice()
      if (len === Number(this.ticketNum)) {
        //用券完全支付
        return technicalServiceFee
      } else {
        //用券不足支付再用套票
        if (this.checkPackageItem && this.checkPackageItem.chooseNum) {
          technicalServiceFee +=
            this.checkPackageItem.technicalServiceFee * (this.ticketNum - len)
        }
        return technicalServiceFee
      }
    }
  }
}
